import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/net/dio_download.dart';
import 'package:flutter_demo/core/common/utils/file_utils.dart';
import 'package:flutter_demo/ui/pages/web/web_view.dart';
import 'package:flutter_vpn/flutter_vpn.dart';
import 'package:flutter_vpn/state.dart';
import 'package:get/get.dart';
import 'package:open_file/open_file.dart';
import 'index.dart';

class VpnPage extends GetView<VpnController> {
  static const String routeName = "/VpnPage";
  VpnPage({Key? key}) : super(key: key);
  final _addressController = TextEditingController();

  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();

  // 主视图
  Widget _buildView() {
    _addressController.text = "47.243.35.109";
    _usernameController.text = "tc";
    _passwordController.text = "12WSXqaz@12";
    FlutterVpnState state = controller.state.state;
    CharonErrorState charonState = controller.state.charonState;
    return ListView(
      padding: const EdgeInsets.all(12),
      children: <Widget>[
        Text('Current State: $state'),
        Text('Current Charon State: $charonState'),
        TextFormField(
          controller: _addressController,
          decoration: const InputDecoration(icon: Icon(Icons.map_outlined)),
        ),
        TextFormField(
          controller: _usernameController,
          decoration: const InputDecoration(
            icon: Icon(Icons.person_outline),
          ),
        ),
        TextFormField(
          controller: _passwordController,
          obscureText: true,
          decoration: const InputDecoration(icon: Icon(Icons.lock_outline)),
        ),
        ElevatedButton(
          child: const Text('Connect'),
          onPressed: () => FlutterVpn.connectIkev2EAP(
            server: _addressController.text,
            username: _usernameController.text,
            password: _passwordController.text,
          ),
        ),
        ElevatedButton(
          child: const Text('Disconnect'),
          onPressed: () => FlutterVpn.disconnect(),
        ),
        ElevatedButton(
          child: const Text('Update State'),
          onPressed: () async {
            var newState = await FlutterVpn.currentState;
            controller.state.state = newState;
            controller.update();
          },
        ),
        ElevatedButton(
          child: const Text('Update Charon State'),
          onPressed: () async {
            var newState = await FlutterVpn.charonErrorState;
            controller.state.charonState = newState!;
            controller.update();
          },
        ),
        ElevatedButton(
          child: const Text('install Certificate'),
          onPressed: () async {
            // DownloadManager.downloadFile("ca.ql.cer").then((value) {
            //    //rootBundle
            //   //下载完成后打开文件
            //   OpenFile.open(value, type: "application/x-x509-ca-cert");
            // });

            FileUtils.assetFileToApplicationFile("cert", "ca.ql.cer")
                .then((value) {
              OpenFile.open(value, type: "application/x-x509-ca-cert");
            });
          },
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VpnController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text("vpn")),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
